package cn.htsyb.synergy.utils;

import jakarta.validation.constraints.NotNull;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
/**
 * 
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @since 1.0
 * @date 2022/5/11
 */
public class FileUtil {

    public static long GB = 1024 * 1024 * 1024;
    public static long MB = 1024 * 1024 * 1024;
    public static long KB = 1024 * 1024 * 1024;

    public static String aaa(String url) {
        return url.replaceAll("\\\\", "/");
    }

    /**
     * 获取文件后缀
     *
     * @param file
     * @return
     */
    public static String getFileSuffix(File file) {
        if (file == null || file.isDirectory()) {
            return null;
        }
        String fileName = file.getName();
        return getFileNameSuffix(fileName);
    }

    /**
     * 路径标识处理
     * 参考 org.springframework.util.ResourceUtils
     *
     * @param path
     * @return
     */
    @Deprecated
    public static String resourcePath(String path) {
        String classPrefix = "classpath:";
        String filePrefix = "file:";
        if (path.startsWith(classPrefix)) {
            path = ClassLoader.getSystemResource("").getPath() + path.substring(classPrefix.length());

        } else {
            path = path.substring(filePrefix.length());
        }
        return new File(path).getPath();//排除file中的偏差字符
    }

    /**
     * 获取文件名的后缀
     *
     * @param fileName
     * @return
     */
    public static String getFileNameSuffix(String fileName) {
        if (fileName != null) {
            int idx = fileName.lastIndexOf('.');
            if (idx > 0 && idx < fileName.length()) {
                return fileName.substring(idx + 1);
            }
        }
        return "";
    }

    /**
     * 获取不带后缀的文件名
     *
     * @param fileName
     * @return
     */
    public static String getFileNameWithOutSuffix(String fileName) {
        if (fileName != null) {
            int idx = fileName.lastIndexOf('.');
            return fileName.substring(0, idx);
        }
        return "";
    }

    /**
     * 列出指定层级的文件目录树
     *
     * @param fileObj
     * @param fileFilter
     * @param maxLayer
     * @return
     */
    /*public static FileTreeVo treeDirOfLayer(@NotNull File fileObj, FileFilter fileFilter, int maxLayer) {
        FileTreeVo fileTreeVo = new FileTreeVo();
        fileTreeVo.setName(fileObj.getName());
        fileTreeVo.setPath(fileObj.getPath());
        if (fileObj.exists() && fileObj.isFile()) {
            fileTreeVo.setType("file");
            return fileTreeVo;
        } else if (fileObj.exists() && fileObj.isDirectory()) {
            fileTreeVo.setType("folder");
            if (maxLayer > 0) {
                File[] listSubFiles = fileFilter == null ? fileObj.listFiles() : fileObj.listFiles(fileFilter);
                if (listSubFiles != null && listSubFiles.length > 0) {
                    for (File subFile : listSubFiles) {
                        fileTreeVo.getChildren().add(treeDirOfLayer(subFile, fileFilter, maxLayer - 1));
                    }
                }
            }
        }
        return fileTreeVo;

    }*/

    /**
     * 列出指定层级的文件列表
     *
     * @param fileObj    当前文件
     * @param fileFilter 文件过滤器
     * @param maxLayer   最大层级
     * @param hasMaxOnly 是否只取最大层级
     * @return
     */
    public static List<File> listFileOfLayer(@NotNull File fileObj, FileFilter fileFilter, int maxLayer, boolean hasMaxOnly) {
        List<File> fileList = new ArrayList<>();
        if (fileObj.exists() && fileObj.isFile()) {
            if (!hasMaxOnly || maxLayer == 0) {
                fileList.add(fileObj);
            }
            return fileList;
        } else if (fileObj.exists() && fileObj.isDirectory()) {
            if (maxLayer > 0) {
                File[] listSubFiles = fileFilter == null ? fileObj.listFiles() : fileObj.listFiles(fileFilter);
                if (listSubFiles != null && listSubFiles.length > 0) {
                    for (File subFile : listSubFiles) {
                        fileList.addAll(listFileOfLayer(subFile, fileFilter, maxLayer - 1, hasMaxOnly));
                    }
                }
            }
        }
        return fileList;
    }

    /**
     * 列出全层级文件列表
     *
     * @param fileObj
     * @param fileFilter
     * @return
     */
    public static List<File> listFileOfLayer(@NotNull File fileObj, FileFilter fileFilter) {
        List<File> fileList = new ArrayList<>();
        if (fileObj.exists() && fileObj.isFile()) {
            fileList.add(fileObj);
            return fileList;
        } else if (fileObj.exists() && fileObj.isDirectory()) {
            File[] listSubFiles = fileFilter == null ? fileObj.listFiles() : fileObj.listFiles(fileFilter);
            if (listSubFiles != null && listSubFiles.length > 0) {
                for (File subFile : listSubFiles) {
                    fileList.addAll(listFileOfLayer(subFile, fileFilter));
                }
            }
        }
        return fileList;
    }

    /**
     * 连接多层文件路径
     *
     * @param pathNameArr
     * @return
     */
    public static String concatPath(String... pathNameArr) {
        String tagPath = null;
        if (pathNameArr != null && pathNameArr.length > 0) {
            for (String pathName : pathNameArr) {
                if (pathName != null && pathName.trim().length() > 0) {
                    boolean pathHasSeparatorChar = pathName.startsWith("/") || pathName.startsWith("\\");
                    if (tagPath == null) {
                        tagPath = pathName;
                    } else {
                        boolean tagHasSeparatorChar = pathName.startsWith("/") || pathName.startsWith("\\");
                        tagPath = tagPath + (pathHasSeparatorChar || tagHasSeparatorChar ? "" : File.separatorChar) + pathName;
                    }
                }
            }
        }
        return tagPath;
    }


    /**
     * 基于文件创建目录
     *
     * @param fileObj
     * @return
     */
    public static void mkdirsPath(File fileObj) {
        if (!fileObj.exists()) {
            fileObj.mkdirs();
        }
    }

    /**
     * 基于文件路径创建目录
     *
     * @param filePath
     */
    public static void mkdirsPath(String filePath) {
        mkdirsPath(new File(filePath));
    }

}
